<!--
* description: 车型工时修改弹窗  2019
* author:xuliwen
* createdDate:2021-2-3
-->
<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="70%"
      @close="closeEdit"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :style-type="$store.state.app.dialogType"
      />
      <div class="filter-container filter-params">
        <el-row :gutter="26">
          <component
            :is="comp.component"
            v-for="comp in tableComponents.filter((o) => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire + comp.compKey : comp.compKey"
            :key="comp.compKey"
            :validrule="comp.validrule"
            :code-field="comp.codeField"
            :text-field="comp.textField"
            :popups-key="comp.compKey"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            :disabled="comp.disabled"
            :is-require="comp.isRequire"
            :span="comp.span || 8"
            :options="comp.options"
            :is-mul="comp.isMul"
            :label-name="comp.labelName"
            :lookuptype="comp.lookuptype"
            :date-options-type="comp.dateOptionsType"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
          />
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from '@/components/wms/mixins/formMixins'
import api from '@/api/se/apiBaseData'
// import request from '@/utils/request'
export default {
  components: {
    dialogHeader: () => import('@/components/dialogHeader') // 弹窗标题
  },
  // 组件混入对象
  mixins: [formMixins],
  props: {
    popupsVisible: {
      type: Boolean,
      default: false
    },
    dynamicEditRowData: {
      type: Object,
      default: () => {}
    },
    wiId: {
      type: String,
      default: ''
    }
  },
  data() {
    const viewPageCode = this.$route.path

    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          icon: 'iconfont  icon-preservation',
          clickEvent: () => this.save(),
          text: '保存'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          icon: 'iconfont icon-preservatio',
          clickEvent: () => this.closeEdit(),
          text: '取消' // 取消
        }
      ],
      // 动态组件-查询条件
       tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '车型：',
              codeField: 'carTypeCode',
              textField: 'carTypeCode',
              component: () => import('@/components/se/carName'),
              type: 'inputText',
              isMust: true,
              isRequire: true,
              span: 8
            },
            {
              compKey: 'compKey2',
              labelName: '车型名称：',
              codeField: 'carConfigCn',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              disabled: true,
              span: 8
            },
            {
              compKey: 'compKey3',
              labelName: '车型品牌：',
              codeField: 'carBrandCn',
              disabled: true,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey5',
              labelName: '工时编码：',
              codeField: 'wiCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              disabled: true,
              span: 8
            },
            {
              compKey: 'compKey7',
              labelName: '标准工时数量：',
              codeField: 'saleWiQty',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              validrule: {
                noRequire: false,
                isformat: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
              },
              isMust: true,
              isRequire: true,
              span: 8
            },
            {
              compKey: 'compKey8',
              labelName: '派工工时数量：',
              codeField: 'wiWorkQty',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isRequire: true,
              validrule: {
                noRequire: false,
                isformat: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
              },
              isMust: true,
              span: 8
            }
          ],
      // 标题
      textMap: {
        edit: '车型工时维护编辑',
        add: '车型工时维护新增'
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: this.$utils.getFormField(this, {
        carTypeCode: '',
        carConfigCode:'',
        carTypeName: '',
        carConfigCn: '',
        carBrandCn: '',
        wiCode: '',
        saleWiQty: '',
        wiWorkQty: '',
        wiId: '',
        wiCartypeId: ''
     }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },
  watch: {
    popupsVisible(val, old) {
      if (val) {
        if (this.popupsState === 'edit') {
          this.$nextTick(() => {
            Object.assign(this.formField, this.dynamicEditRowData)
          })
        } else {
          for (const key in this.formField) {
            this.formField[key] = key == 'wiId' ? this.wiId : ''
          }
        }
        this.formField.wiCode = this.dynamicEditRowData.wiCode
      }
    }
  },
  methods: {
    closeEdit() {
      this.$emit('update:popupsVisible', false)
      this.editPopupsKey = this.$utils.generateId()
    },
    // 新增/编辑保存按钮
    save() {
      this.$utils.validataMoth(this, 'validpopup')
      if (this.valid) {
        if (this.popupsState === 'edit') {
          this.editSave()
        } else {
          this.addSave()
        }      
      }
    },
    // 编辑保存
    editSave() {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: api.updateWorkingHoursAndCarType,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: response.msg,
            type: 'success',
            duration: 1000
          })
          this.popupsVisible = false
          this.$nextTick(() => {
            this.$parent.$parent.queryTable(1)
          })
        } else {
          // 保存失败
          this.$message({
            message: response.msg,
            type: 'warn',
            duration: 1000
          })
        }
        this.popupsVisible = false
      })
    },
    // 新增保存
    addSave() {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: api.saveWorkingHoursAndCarType,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: response.msg,
            type: 'success',
            duration: 1000
          })
          this.$nextTick(() => {
            this.$parent.$parent.queryTable(1)
          })
        //  this.closeEdit()
        } else {
          // 保存失败
          this.$message({
            message: response.msg,
            type: 'warn',
            duration: 1000
          })
        }
        this.popupsVisible = false
      })
    }
  }
}
</script>
<style type="text/css" scoped>
.filter-params .el-col label .curName {
  width: 150px;
}
</style>
